package com.baixiaowen.xiaoaointerview.Java编程功底篇.五种手写排序.冒泡排序;

import com.baixiaowen.xiaoaointerview.Java编程功底篇.五种手写排序.Helper;
import com.baixiaowen.xiaoaointerview.Java编程功底篇.五种手写排序.IMutableSorter;

/**
 * 冒泡排序
 *
 * @author Baixiaowen
 */
public class BubbleSort implements IMutableSorter {

    @Override
    public void sort(int[] A) {
        // 2 2 1 1 
        for (int i = A.length - 1; i >= 0 ; i--) {
            // 找到 0 - i 间的最大元素放到 A[i]
            bubble(A, 0, i + 1);
        }
    }

    private void bubble(int[] A, int i, int j) {
        for (int k = 0; k < j - 1; k++) {
            if (A[k] > A[k + 1]) {
                Helper.swap(A, k, k + 1);
            }
        }
    }
}
